﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Windows.Forms.DataVisualization.Charting;
using System.IO;
using Wicresoft.UnifyShow.Business;

namespace Wicresoft.UnifyShow2.Portal.Extensions
{
    public class ChartResult : ActionResult
    {
        private readonly Chart _chart;
        private readonly string _contentType;
        //private string _fileDownloadName;
        //public string FileDownloadName
        // {
        //   get { return _fileDownloadName ?? String.Empty; }
        //   set { _fileDownloadName = value; }
        //  }


        public ChartResult(Chart chart, string contentType = "image/gif")
        {
            if (chart == null)
                throw new ArgumentNullException("chart");

            _chart = chart;
            _contentType = contentType;

            if (string.IsNullOrEmpty(_contentType))
                _contentType = "image/gif";
        }

        public Chart Chart
        {
            get { return _chart; }
        }

        public string Format
        {
            get { return _contentType; }
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            HttpResponseBase response = context.HttpContext.Response;
            response.ContentType = _contentType;

            bool isEmpty = true;
            foreach (var item in _chart.Series)
            {
                foreach (var point in item.Points)
                {
                    if (point.YValues[0] != 0)
                    {
                        isEmpty = false;
                        break;
                    }
                }
                if (isEmpty == false) break;
                //if (item.Points.Count != 0) isEmpty = false;
            }

            //if (!String.IsNullOrEmpty(FileDownloadName))
            //{
            //    // From RFC 2183, Sec. 2.3:
            //    // The sender may want to suggest a filename to be used if the entity is
            //    // detached and stored in a separate file. If the receiving MUA writes
            //    // the entity to a file, the suggested filename should be used as a
            //    // basis for the actual filename, where possible.
            //    string headerValue = ContentDispositionUtil.GetHeaderValue(FileDownloadName);
            //    context.HttpContext.Response.AddHeader("Content-Disposition", headerValue);
            //}
            if (isEmpty == true)
            {
                byte[] empty = (byte[])CacheHelper.GetCache("EmptyReport");
                response.OutputStream.Write(empty, 0, empty.Length);
            }
            else
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    _chart.SaveImage(ms, ChartImageFormat.Gif);
                    byte[] file = ms.ToArray();
                    response.OutputStream.Write(file, 0, file.Length);
                }
            }
        }
    }
}